# 图片处理的类

import base64  
import re
import requests
from PIL import Image  
from io import BytesIO  

import logging  
logging.basicConfig(level=logging.INFO)  


def base64ToBytes(strbase64):
    if strbase64 is None:
        logging.warn('参数value不允许为空')
        return None

    strbase642= strbase64
    # 处理 开头类型的串 data:image/jpg;base64,

    strregex='data:image/(jpg|png);base64,'
    if re.match(strregex, strbase64):
        strbase642=re.sub(strregex,'',strbase64)

    if strbase642 is not None: 
        bytes=base64.b64decode(strbase642)

    return bytes

# 获取base64的图片类型 ，jpg png 等
# def getBase64ImgType(strbase64,refindstr='data:image/(jpg|png);base64,'):
#     res=RegexUtil.findByGroup(str=strbase64, refindstr=refindstr)
#     return res

def bytesToPILImg(bytes):
    pilImg = Image.open(BytesIO(bytes)) 
    return pilImg
    

    
